package com.jic.purchase.controller;

import com.jic.common.base.vo.PageResult;
import com.jic.purchase.constant.consist.ResponseCode;
import com.jic.purchase.model.vo.request.UpdatePurchaseContractRequest;
import com.jic.purchase.model.vo.response.QueryInfoPurchaseContractResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.stream.Collectors;

import com.jic.common.base.vo.RestResult;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import com.jic.purchase.model.vo.request.PurchaseContractRequest;
import com.jic.purchase.model.vo.response.PurchaseContractResponse;
import com.jic.purchase.model.entity.PurchaseContract;
import com.jic.purchase.service.PurchaseContractService;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-06-03 16:37:08
 */
@RestController
@Slf4j
@RequestMapping("/purchaseContract")
@Api(tags = " [采购合同表]控制类-PC")
public class PurchaseContractController {
    @Autowired
    private PurchaseContractService service;

    /**
     * 新增记录
     *
     * @param request 新增[采购合同表]请求参数
     * @return PurchaseContractResponse 新增[采购合同表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[采购合同表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody PurchaseContractRequest request) throws Throwable {
        PurchaseContract purchaseContract = new PurchaseContract();
        BeanUtils.copyProperties(request, purchaseContract);
        Long flag = service.save(purchaseContract);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[采购合同表]请求参数
     * @return PurchaseContractResponse 新增或修改[采购合同表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[采购合同表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody PurchaseContractRequest request) throws Throwable {
        PurchaseContract purchaseContract = new PurchaseContract();
        BeanUtils.copyProperties(request, purchaseContract);
        long flag = service.saveOrUpdate(purchaseContract);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[采购合同表]请求参数
     * @return PurchaseContractResponse 删除[采购合同表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[采购合同表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody PurchaseContractRequest request) throws Throwable {
        PurchaseContract purchaseContract = new PurchaseContract();
        BeanUtils.copyProperties(request, purchaseContract);
        long flag = 0;
        if (request.getIds() != null && request.getIds() != "" && request.getIds() != "null") {
            for (int i = 0; i < request.getIds().split(",").length; i++) {
                purchaseContract.setId(Long.parseLong(request.getIds().split(",")[i]));
                flag = service.deleteByPrimaryKeyLogically(purchaseContract);
            }
        } else {
            flag = service.deleteByPrimaryKeyLogically(purchaseContract);
        }
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[采购合同表]请求参数
     * @return PurchaseContractResponse 修改[采购合同表]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[采购合同表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody PurchaseContractRequest request) throws Throwable {
        PurchaseContract purchaseContract = new PurchaseContract();
        BeanUtils.copyProperties(request, purchaseContract);
        long flag = service.updateByPrimaryKeySelective(purchaseContract);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[采购合同表]详情请求参数
     * @return PurchaseContractResponse 查询[采购合同表]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[采购合同表]详情")
    @ResponseBody
    public RestResult<PurchaseContractResponse> getByPrimaryKey(@Validated @RequestBody PurchaseContractRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            PurchaseContract purchaseContract = new PurchaseContract();
            BeanUtils.copyProperties(request, purchaseContract);
            PurchaseContract result = service.getByPrimaryKey(purchaseContract);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            PurchaseContractResponse bo = new PurchaseContractResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new PurchaseContractResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageResult<PurchaseContract>> listPage(@RequestBody PurchaseContractRequest request) throws Throwable {
        PurchaseContract param = new PurchaseContract();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }

    /**
     * PURCHASE_CONTRACT 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [采购合同表] ")
    @ResponseBody
    public RestResult<List<PurchaseContractResponse>> listPurchaseContractAllRecord(@RequestBody PurchaseContractRequest request) throws Throwable {
        PurchaseContract param = new PurchaseContract();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<PurchaseContract> list = service.listAllRecord(param);
        List<PurchaseContractResponse> responseList = list.stream().map(temp -> {
            PurchaseContractResponse bo = new PurchaseContractResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }


    @PostMapping("/updatePurchaseContract")
    @ResponseBody
    public RestResult<Boolean> updatePurchaseContract(@RequestBody UpdatePurchaseContractRequest request){
        return service.updatePurchaseContract(request);
    }

    @PostMapping("/queryInfoPurchaseContract")
    @ResponseBody
    public RestResult<QueryInfoPurchaseContractResponse> queryInfoPurchaseContract(@RequestBody PurchaseContractRequest request){
        return service.queryInfoPurchaseContract(request);
    }

}

